package com.tanhua.app.controller;

import com.tanhua.app.Interceptor.UserHolder;
import com.tanhua.app.service.UserInfoService;
import com.tanhua.app.service.UserService;
import com.tanhua.model.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 发送验证码
     * 路径：POST/user/login
     * 请求参数： phone	string
     * @param params
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity<String> login(@RequestBody Map<String,String> params) {
        // 1.获取手机号
        String mobile = params.get("phone");

        // 2. 发送验证码
        userService.sendCode(mobile);

        // 3. 返回数据
        return ResponseEntity.ok("");
    }


    /**
     * 用户登陆
     *
     * 路径：/user/loginVerification
     *
     * 参数： phone	string  || verificationCode	string
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map params) {
        // 1. 获取请求参数
        String mobile = params.get("phone").toString();
        String code = params.get("verificationCode").toString();

        // 2. 调用UserService的方法，获取返回的参数
        Map retMap = userService.loginVerification(mobile, code);

        // 3. 返回参数
        return ResponseEntity.ok(retMap);
    }


    /**
     * 03 首次登录---完善资料
     *
     * POST/user/loginReginfo
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo) {
/*                                       @RequestHeader("Authorization") String token) {
        // 1. token鉴权
        boolean verifyToken = JwtUtils.verifyToken(token);

        // 2. 鉴权为false,直接返回
        if (!verifyToken) {
            return ResponseEntity.status(HttpStatus.HTTP_UNAUTHORIZED).body(null);
        }

        // 3. 获取token中用户的id，设置到userInfo中
        Long id = Long.parseLong(JwtUtils.getClaims(token).get("id").toString());*/
        // 从线程变量中获取数据
        Long id = UserHolder.getUserId();
        userInfo.setId(id);

        // 4. 执行业务层的代码
        userInfoService.save(userInfo);

        // 5. 返回结构
        return ResponseEntity.ok(null);
    }


    /**
     * 04 首次登录---补充头像
     * POST/user/loginReginfo/head
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity head(MultipartFile headPhoto) throws IOException {
        /*@RequestHeader("Authorization") String token,
        // 1. token健全
        boolean verifyToken = JwtUtils.verifyToken(token);

        // 2. 校验
        if (!verifyToken) {
            return ResponseEntity.status(HttpStatus.HTTP_UNAUTHORIZED).body(null);
        }

        // 3. 获取id
        Long id = Long.parseLong(JwtUtils.getClaims(token).get("id").toString());*/

        // 从ThreadLocal中获取数据
        Long id = UserHolder.getUserId();

        UserInfo userInfo = new UserInfo();
        userInfo.setId(id);
        // 4. 调用方法
        userInfoService.updateHead(headPhoto, userInfo);

        // 5. 返回结果
        return ResponseEntity.ok(null);

    }

}
